package com.andnnl.jiaowei;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

import static java.util.Comparator.comparing;

/**
 * Created by chenss on 2017/9/13.
 */
public class JiaoWei {


    public static void main(String[] args) {
        List<Lac> lacList = new ArrayList<>();
        List<Indicatrix> indList = new ArrayList<>();
        Map<String, Integer> lacMap = new HashMap<>();
//        lacMap.put("A", 2D);
//        lacMap.put("B", 3D);
//        lacMap.put("C", 5D);
//        lacMap.put("D", 7D);

        lacMap.put("A1", 48);
        lacMap.put("A2", 51);
        lacMap.put("A3", 39);
        lacMap.put("A4", 47);
        lacMap.put("A5", 52);
        lacMap.put("A6", 51);
        lacMap.put("A7", 44);
        lacMap.put("A8", 40);
        lacMap.put("A9", 45);
        lacMap.put("A10", 60);
        lacMap.put("A11", 39);
        lacMap.put("A12", 47);
        lacMap.put("A13", 50);
        lacMap.put("A14", 42);
        lacMap.put("A15", 43);
        //初始化数据
//        lacMap.forEach((k, v) -> {
//            for (int i = 0; i < v; i++) {
////                System.out.println(k+" "+v);
//                lacList.add(new Lac(k, 0D));
//            }
//        });

        //求和
        int total = lacMap.values().stream().reduce(0, (a, b) -> a + b);
        System.out.println("total:" + total);
        //求每一项的时间间隔=总和/每一项
        Map<String, Double> intervalMap = lacMap.entrySet().stream()
                .collect(Collectors.toMap(s -> s.getKey(), s -> 1000D/s.getValue()));//(s.getKey(),(total / s.getValue())
//        Map<String, Double> intervalMap = lacMap.entrySet().stream().collect(Collectors.toMap(s -> s.getKey(), s -> 1D*total/s.getValue()));//(s.getKey(),(total / s.getValue())
        System.out.println(intervalMap);


        //初始化数据
        int records = 3984111;
        for (int i = 0; i < records / lacMap.keySet().size(); i++) {
            lacMap.keySet().forEach(a -> lacList.add(new Lac(a, 0D)));
        }


        Map<String, List<Lac>> map2 = lacList.stream().collect(Collectors.groupingBy((Lac l) -> l.name, Collectors.toList()));
//        System.out.println(map2);
        map2.forEach((k, list) -> {
            double interval = intervalMap.get(k);
            for (int i = 0; i < list.size(); i++) {
                if (i == 0) {
                    list.get(i).time = (i + 1) * interval / 2;//第一个为时间间隔的一半
                } else {
                    list.get(i).time = list.get(i - 1).time + interval;//第二个的时间等于第一个时间加上间隔时间
                }
            }
        });
        //排序并打印出来
        Stream<Lac> listStream = map2.values().stream().flatMap(a -> a.stream()).sorted(comparing(lac -> lac.time));
        List<Lac> lacs = listStream.collect(Collectors.toList());
        lacs.forEach(a -> System.out.println(a.name + "\t : " + round(a.time, 2)));

//        for(int i=0;i<lacs.size()/total;i++){
//            Lac a=lacs.get((int) (i*total));
//            int endIdx=(int) ((i+1)*total-1);
//            if(endIdx>=lacs.size()){
//                endIdx=lacs.size()-1;
//            }
//            Lac b=lacs.get(endIdx);
//            System.out.println("第"+(i+1)+"组所花时间："+(b.time-a.time));
//        }
        //绑定下标，每隔total个分组
        Map<Integer, List<Lac>> mapG = IntStream.range(0, lacs.size())
                .boxed().map(i -> new AbstractMap.SimpleEntry<Integer, Lac>(i, lacs.get(i)))
                .collect(Collectors.groupingBy(entry -> entry.getKey() / (int) total
                        , Collectors.mapping(entry -> entry.getValue(), Collectors.toList())));
//        mapG.forEach((k, v) -> System.out.println(k + "\t" + (v.get(v.size() - 1).time - v.get(0).time)+"\t"+v.stream().map(a->a.name).distinct().collect(Collectors.toList())));


        System.out.println(records / lacMap.keySet().size()/39);
    }

    /**
     * 提供精确的小数位四舍五入处理
     *
     * @param v1    需要四舍五入的数字
     * @param scale 小数点后保留几位
     * @return 四舍五入后的结果
     */
    public static double round(double v1, int scale) {

        if (scale < 0) {
            throw new IllegalArgumentException("scale不能小于0");
        }
        BigDecimal b = new BigDecimal(Double.toString(v1));
        BigDecimal one = new BigDecimal("1");
        return b.divide(one, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
    }
}

class Lac {
    String name;
    Double time;

    public Lac(String name, Double time) {
        this.name = name;
        this.time = time;
    }
}

/**
 * 指标类
 */
class Indicatrix{
    String lac;
    String ci;
    int secNum;
    int totalCount;

    public Indicatrix(String lac, String ci, int secNum, int totalCount) {
        this.lac = lac;
        this.ci = ci;
        this.totalCount = totalCount;
        this.secNum = secNum;
    }

    public Indicatrix(String lac, int secNum, int totalCount) {
        this.lac = lac;
        this.totalCount = totalCount;
        this.secNum = secNum;
    }
}


class LacInterval {
    String name;
//    String ci;
    Double interval;

    public LacInterval(String name, Double interval) {
        this.name = name;
        this.interval = interval;
    }
}
